<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/8
 * Time: 19:08
 */
namespace Crm\Controller;
use Think\Controller;

class OaUserController extends Controller
{
//    public function oaUser(){
//        if(!S('oa_user')){
//            $content = file_get_contents('http://oa.sxqtwy.com:88/interface/users.php?USER_ID=crm&PASSWORD=crm123&FROM_ID=admin&TO_ID=admin&CONTENT=1212');
//            $oa_user = json_decode($content,JSON_UNESCAPED_UNICODE);
//            S('oa_user',$oa_user);
//        }
//        foreach(S('oa_user') as $k=>$v){
//            if($v['USER_ID'] == 'admin'){
//                $data['user_name'] = 'Administrator';
//                $data['id'] = 1;
//                $data['real_name'] = $v['USER_NAME'].'1';
//                $data['oa_user_id'] = $v['USER_ID'];
//                $data['oa_user_name'] = $v['USER_NAME'];
//                $data['oa_user_priv_name'] = $v['USER_PRIV_NAME'];
//                $data['password'] = md5(123456);
//                D('user')->data($data)->save();
//                unset($data);
//            }else{
//                $data['user_name'] = $v['USER_NAME'];
//                $data['real_name'] = $v['USER_NAME'];
//                $data['oa_user_id'] = $v['USER_ID'];
//                $data['oa_user_name'] = $v['USER_NAME'];
//                $data['oa_user_priv_name'] = $v['USER_PRIV_NAME'];
//                $data['password'] = md5(123456);
//                $data['is_super'] = 0;
//                $data['is_active'] = 1;
//                $data['create_time'] = time();
//                D('user')->data($data)->add();
//                unset($data);
////                var_dump(D('user')->getDbError());exit;
//            }
//        }
//        $this->ajaxReturn(S('oa_user'));
//    }

    public function getUser(){
        $req = I('param.');
        if($req['DEPT_ID']){
            $where = array('DEPT_ID'=>$req['DEPT_ID']);
        }else{
            $where = array();
        }
        $data = D('OaUser')->getUser($where);
        $this->ajaxReturn($data);
    }

    public function getCs(){
        $req = I('param.');
        if($req['DEPT_ID']){
            $where = array('DEPT_ID'=>$req['DEPT_ID'],'USER_PRIV'=>array('in',array('62','65')));
        }else{
            $where = array();
        }
        $data = D('OaUser')->getUser($where);
        $req['return'] = $req['return'] ? $req['return'] : 'ajax';
        if($req['return'] == 'display'){
            $this->assign('data',$data);
            $this->display();
        }elseif($req['return'] == 'ajax'){
            $this->ajaxReturn($data);
        }
    }

    public function search(){
        $param = $this->getParam();
        $modelName = $this->modelName;
        $scope = array();
        $relation = array();
        $search = $this->getSearch();
        $page = $this->getPage();
        $data = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
        echo D($modelName)->getLastSql();
        if($data){
            $res['stat'] = 1;
            $res['data'] = $data;
            $res['count'] = $data = D($modelName)->where($param)->where($search)->scope($scope)->count();
            $res['msg'] = '查找到1条数据。';
        }elseif($data == null){
            $res['stat'] = 1;
            $res['data'] = array();
            $res['count'] = 0;
            $res['msg'] = '查找到0条数据。';
        }else{
            $res['stat'] = 0;
            $res['data'] = $data;
            $res['msg'] = '查找失败！';
        }
        $res['recordsTotal'] = $res['count'];
        $res['recordsFiltered'] = $res['count'];
        $this->ajaxReturn($res);
    }

    public function read(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $field = array();
            $relation = array();
            if($param[$pk]){
                $data = D($modelName)->field($field)->relation($relation)->find($param[$pk]);
            }else{
                $data = D($modelName)->where($param)->field($field)->relation($relation)->find();
            }
            if($data){
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['msg'] = '查找到1条数据。';
            }elseif($data == null){
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找到0条数据。';
            }else{
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找失败！';
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }
}